package cn.wolfcode.trip.app.util;

import cn.wolfcode.trip.base.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

public class UserContext {
    public static final String USER_IN_SESSION = "USER_IN_SESSION";

    //获取session对象
    public static HttpSession getSession(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest().getSession();
    }

    //将当前用户添加到session中
    public static void setCurrentUser(User user){
        getSession().setAttribute(USER_IN_SESSION,user);
    }

    //获取session中当前登录的用户
    public static User getCurrentUser(){
        return (User) getSession().getAttribute(USER_IN_SESSION);
    }


}
